package com.sport.controller;


import com.sport.constant.MessageConstant;
import com.sport.result.Result;
import com.sport.service.TeamService;
import com.sport.service.UserService;
import com.sport.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;


@RestController
@Slf4j
@RequestMapping("/user/upload")
@Api(tags = "文件上传接口")
public class UploadController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private UserService userService;
    private TeamService teamService;

    @ApiOperation("头像上传")
    @PostMapping("/uploadAvatar")
    public Result upload(MultipartFile file) {
        try {
            //构造文件路径
            String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String path = aliOssUtil.upload(file.getBytes(), UUID.randomUUID().toString() + extension);
            if (userService.updateAvatar(path) != 0) {
                return Result.success(path);
            }
            return Result.error("头像上传失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

    @ApiOperation("学生认证")
    @PostMapping("/studentVerification")
    public Result studentVerification(MultipartFile file) {
        try {
            //构造文件路径
            String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String path = aliOssUtil.upload(file.getBytes(), UUID.randomUUID().toString() + extension);
            if (userService.studentVerification(path) != 0) {
                return Result.success(path);
            }
            return Result.error("认证失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

    @ApiOperation("裁判认证")
    @PostMapping("/judgeVerification")
    public Result judgeVerification(MultipartFile file) {
        try {
            //构造文件路径
            String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String path = aliOssUtil.upload(file.getBytes(), UUID.randomUUID().toString() + extension);
            if (userService.judgeVerification(path) != 0) {
                return Result.success(path);
            }
            return Result.error("认证失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

    @ApiOperation("战队头像上传")
    @PostMapping("/uploadTeamAvatar")
    public Result uploadTeamAvatar(MultipartFile file) throws IOException {
        try {
            //构造文件路径
            String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String path = aliOssUtil.upload(file.getBytes(), UUID.randomUUID().toString() + extension);
            return Result.success(path);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}